package com.jx.cashier.rest.http;

import android.annotation.SuppressLint;
import android.content.Context;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ta.util.http.AsyncHttpClient;
import com.ta.util.http.AsyncHttpResponseHandler;
import com.ta.util.http.FileHttpResponseHandler;
import com.ta.util.http.RequestParams;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class JxDccRest {
	
	private ObjectMapper mapper = new ObjectMapper();
	private AsyncHttpClient httpClient = new AsyncHttpClient();
	
	public JxDccRest(){
		httpClient.setTimeout( 30 * 1000 );  //default 0.5 mins
	}
	
	public void getForObject(String url, RequestParams params, AsyncHttpResponseHandler response){
		httpClient.get(url, params, response);
	}
	
	public void postFile(Context context, String url, String filePath, AsyncHttpResponseHandler response){
		try {
			File file = new File(filePath);
			if ( !file.exists() ) {
				return;
			}
//			FileEntity entity = new FileEntity(file, "UTF-8");
//			httpClient.post(context, url, entity, null, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void postForObject(Context context, String url, Object request, AsyncHttpResponseHandler response){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		mapper.setDateFormat(df);
		try {
			String body = mapper.writeValueAsString(request);
//			StringEntity entity = new StringEntity(body, "UTF-8");
//			httpClient.post(context, url, entity, "application/json", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void deleteForObject(String url, AsyncHttpResponseHandler response){
		httpClient.delete(url, response);
	}
	@SuppressLint("SimpleDateFormat")
	public void putForObject(Context context, String url, Object request, AsyncHttpResponseHandler response){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		mapper.setDateFormat(df);
		
		try {
			String body = mapper.writeValueAsString(request);
//			StringEntity entity = new StringEntity(body, "UTF-8");
//			httpClient.put(context, url, entity, "application/json", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void downloadForObject(String url, RequestParams params, FileHttpResponseHandler response){
		httpClient.download(url, params, response);
	}
}
